// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
var ClickManager = require('ClickManager')
var Api = require('Api')
var UnitTools = require('UnitTools')
var CreatorHelper = require('CreatorHelper')
var TableManager = require('TableManager')

cc.Class({
    extends: cc.Component,

    properties: {
        rechargeList1: cc.Node,
        rechargeList2: cc.Node,

        webViewOpenFlag: false,
        rechargeWebView: cc.Node,
        webView: null
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        var self = this
        this.exitButtonNode = cc.find('ExitButton', this.node)
        CreatorHelper.setNodeClickEvent(this.exitButtonNode, () => {
            this.exitButtonNode.active = false
            this.rechargeWebView.active = false
            this.rechargeWebView.removeComponent(cc.WebView)
        })

        for (let i = 0; i < this.rechargeList1.children.length; i++) {
            let childNode = this.rechargeList1.children[i]
            childNode.i = i

            CreatorHelper.setNodeClickEvent(childNode, () => {
                this.goToRechargePage(childNode.i)
            })
        }

        for (let i = 0; i < this.rechargeList2.children.length; i++) {
            let childNode = this.rechargeList2.children[i]
            childNode.i = i

            CreatorHelper.setNodeClickEvent(childNode, () => {
                this.goToRechargePage(childNode.i+4)
            })
        }
    },

    start () {

    },

    // 跳转充值页面
    goToRechargePage (index) {
        index++
        this.exitButtonNode.active = true
        this.rechargeWebView.active = true

        let token = cc.sys.localStorage.getItem('__TOKEN__')

        this.webView = this.rechargeWebView.addComponent(cc.WebView)
        let param = '?token='+token+'&money='+index
        this.webView.url = Api.recharge + param

        // webview加载回调
        let webviewEventHandler = new cc.Component.EventHandler()
        webviewEventHandler.target = this.node
        webviewEventHandler.component = this
        webviewEventHandler.handler = 'rechargePageCallback'
        webviewEventHandler.customEventData = false

        // 监听webView传递过来的数据
        if (cc.sys.isBrowser) {
            var count = 0
            window.addEventListener('message', (e) => {
                count++
                if (count > 2) return false
                console.log('web回调:', e)
                webviewEventHandler.customEventData = true
                if (e.data.ok) {
                    this.webViewOpenFlag = true
                } else if (e.data.close) {
                    this.node.active = false
                    this.rechargeWebView.active = false
                    TableManager.alertModel(e.data.msg)
                }
            })
        }

        this.webView.webviewEvents.push(webviewEventHandler)
    },

    // 充值页面回调
    rechargePageCallback (webview, eventType, customEventData) {
        if (!this.webViewOpenFlag && !customEventData) {
            console.log('页面加载失败啦')
            TableManager.alertModel('页面加载失败啦')
            this.rechargeWebView.active = false
            this.rechargeWebView.removeComponent(cc.WebView)
        }
    },

    // update (dt) {},
});
